<?php
/**
 * @title 2074. 反转偶数长度组的节点
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a singly-linked list.
 * class ListNode {
 *     public $val = 0;
 *     public $next = null;
 *     function __construct($val = 0, $next = null) {
 *         $this->val = $val;
 *         $this->next = $next;
 *     }
 * }
 */
class Solution {

    /**
     * @param ListNode $head
     * @return ListNode
     */
    function reverseEvenLengthGroups($head) {
        /**
         * @since 2024-05-13 组顺序，节点在组内顺序
         * @since 2024-05-13 偶数长度组节点栈
         * @since 2024-05-13 上一个节点
         */
        $groupNo = $i = 1;
        $stack = [];
        $prev = null;

        /**
         * @since 2024-05-13
         */
        $node = $head;
        while($node){
            /**
             * @since 2024-05-13 节点入栈
             */
            $stack[] = $node;
            $nextNode = $node->next;

            /**
             * @since 2024-05-13 一个自然组数量足够
             */
            if($i == $groupNo){
                /**
                 * @since 2024-05-13 反转偶数长度组
                 */
                if($groupNo%2 == 0){
                    while($stack){
                        $prev->next = array_pop($stack);
                        $prev = $prev->next;
                    }
                    $prev->next = $nextNode;
                } else {
                    /**
                     * @since 2024-05-13 奇数长度组最后一个节点
                     */
                    $prev = $node;
                    $stack = [];
                }

                /**
                 * @since 2024-05-13 重置组顺序
                 */
                $i=1;
                $groupNo++;
            } else {
                $i++;
            }

            $node = $nextNode;
        }

        /**
         * @since 2024-05-13 收官处理，偶数长度组
         */
        if(count($stack)%2 == 0){
            while($stack){
                $prev->next = array_pop($stack);
                $prev = $prev->next;
            }
            $prev->next = null;
        } else {}

        /**
         * @return
         */
        return $head;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202405/2024.05.13_3.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
